package com.efast.dmom.aps.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.aps.bean.CustomerPart;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.datahandlers.CustomerPartDataHandler;
import com.efast.dmom.aps.service.CustomerPartService;

@Service
public class CustomerPartServiceImpl implements CustomerPartService {

	@Autowired
	private CustomerPartDataHandler customerPartDataHandler;

	@Override
	public ResultBean save(CustomerPart customerPart) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (!StringUtil.isNotBlank(customerPart.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查名称是否填写!");
			return resultBean;
		}
		if (customerPart.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (customerPart.getInnerData().getCustomerId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查客户是否填写!");
			return resultBean;
		}
		CustomerPart customerPartByName = customerPartDataHandler.getByName(customerPart.getInnerData().getName());
		if(customerPartByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("名称已存在，请重填!");
			return resultBean;
		}
		CustomerPart customerPartData = customerPartDataHandler.getCustomerPartByCustomerIdAndPartId(
				customerPart.getInnerData().getCustomerId(), customerPart.getInnerData().getPartId());
		if (customerPartData.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("客户物料关系已存在，请重填!");
			return resultBean;
		}
		customerPartDataHandler.save(customerPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户物料关系保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(CustomerPart customerPart) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (!StringUtil.isNotBlank(customerPart.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查名称是否填写!");
			return resultBean;
		}
		if (customerPart.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (customerPart.getInnerData().getCustomerId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查客户是否填写!");
			return resultBean;
		}
		CustomerPart customerPartById = customerPartDataHandler.getById(customerPart.getInnerData().getId());
		if (customerPartById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据不存在!");
			return resultBean;
		}
		if (!customerPartById.getInnerData().getName().equals(customerPart.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("名称不可修改!");
			return resultBean;
		}
		if (customerPart.getInnerData().getPartId() != customerPartById.getInnerData().getPartId()
				&& customerPart.getInnerData().getCustomerId() != customerPartById.getInnerData().getCustomerId()) {
			CustomerPart customerPartData = customerPartDataHandler.getCustomerPartByCustomerIdAndPartId(
					customerPart.getInnerData().getCustomerId(), customerPart.getInnerData().getPartId());
			if (customerPartData.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后客户物料关系已存在，不可进行修改!");
				return resultBean;
			}
		}
		customerPartDataHandler.update(customerPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户物料关系修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(CustomerPart customerPart) {
		ResultBean resultBean = new ResultBean(true, ' ');
		customerPartDataHandler.delete(customerPart);
		resultBean.setSuccess(true);
		resultBean.setMessage("客户物料关系删除成功!");
		return resultBean;
	}

	@Override
	public CustomerPart getById(long id) {
		return customerPartDataHandler.getById(id);
	}

	@Override
	public CustomerPart getByName(String name) {
		return customerPartDataHandler.getByName(name);
	}

	@Override
	public List<CustomerPart> getCustomerPartByCustomerId(long customerId) {
		return customerPartDataHandler.getCustomerPartByCustomerId(customerId);
	}

	@Override
	public List<CustomerPart> getCustomerPartByPartId(long partId) {
		return customerPartDataHandler.getCustomerPartByPartId(partId);
	}

}
